package x.ovo.wechat.bot;

import x.ovo.wechat.bot.common.entity.event.MessageEvent;
import x.ovo.wechat.bot.common.entity.message.Message;
import x.ovo.wechat.bot.common.event.Event;
import x.ovo.wechat.bot.common.event.EventListener;

import java.time.Duration;
import java.time.Instant;
import java.util.Objects;

public class KouhaiListener extends EventListener<MessageEvent<Message>> {

    private static long lastTime = 0;

    @Override
    public boolean support(Event<?> event) {
        return event instanceof MessageEvent
                && ((Message)event.getSource()).isGroup()
                && Objects.nonNull(((Message) event.getSource()).getMember())
                && ("traveler".equals(((Message) event.getSource()).getMember().getNickName()));
    }

    @Override
    public void onEvent(MessageEvent<Message> event) {
        if (Duration.between(Instant.now(), Instant.ofEpochMilli(lastTime)).abs().toMinutes() > 10) {
            this.context.getApi().sendText(event.getSource().getFromUser().getUserName(), "口嗨怪出现了~");
        }
        lastTime = System.currentTimeMillis();
    }
}
